<?php namespace web\home\controller;
/** .-------------------------------------------------------------------
 * |  Software: [HDPHP framework]
 * |      Site: www.hdphp.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/
//	前台首页控制器
class Listnew extends Common{
	//首页
	public function index() {
		$cid=Q('get.cid',0,'intval');
		$tid=Q('get.tid',0,'intval');
//		实例化分类
		$cate=new \system\model\Category;
//		获取所有分类
		$cateData=$cate->get();
//		获取当前请求的分类的子集id，调用分类模型中的getson方法
		$cids=$cate->getSon($cateData,$cid);
//		把自己也压入
		$cids[]=$cid;
		
//		判断是哪个控制器请求
		if($cid){
			$headData=array(
				'type'=>'分类名',
//				pluck取指定的字段内容
				'title'=>Db::table('category')->where('cid',$cid)->pluck('cname'),
//				统计当前分类和子分类下得文章数量
				'total'=>Db::table('article')->where('is_recycle',0)->whereIn('category_cid',$cids)->count(),
			);
//			获取当前分类数据
			$artData=Db::table('article')->join('category','category_cid','=','cid')
			->where('is_recycle',0)
			->whereIn('category_cid',$cids)
			->orderBy('sendtime','DESC')
			->get();

//			将标签数据压入得到的数组中
			foreach($artData as $k=>$v){
				$artData[$k]['tag']=Db::table('article_tag')
			->join('tag','tag_tid','=','tid')->field(array('tid','tname'))
			->where('article_aid',$v['aid'])->get();
			}	
//			分页
//			$arcData=Db::table('article')->join('category','category_cid','=','cid')
//			->where('is_recycle',0)
//			->whereIn('category_cid',$cids)
//			->orderBy('sendtime','DESC')
//			->count();
//			$page=Page::row(1)->make($arcData);
//			$arcData=Db::table('article')->join('category','category_cid','=','cid')
//			->where('is_recycle',0)
//			->orderBy('sendtime','DESC')->limit(Page::limit())->get();
						
//		var_dump($page);			
		}
		
		if($tid){
			
			$headData=array(
				'type'=>'标签名',
//				pluck取指定的字段内容
				'title'=>Db::table('tag')->where('tid',$tid)->pluck('tname'),
//				统计当前分类和子分类下得文章数量
				'total'=>Db::table('article_tag')->join('article','article_aid','=','aid')->where('is_recycle',0)->where('tag_tid',$tid)->count(),
			);
//			通过中间表获取文章数据
			$aids=Db::table('article_tag')->where('tag_tid',$tid)->lists('article_aid');		
			$artData=Db::table('article')->join('category','category_cid','=','cid')
			->where('is_recycle',0)
			->whereIn('aid',$aids)
			->orderBy('sendtime','DESC')
			->get();
//			将标签压入
			foreach($artData as $k=>$v){
				$artData[$k]['tag']=Db::table('article_tag')
				->join('tag','tag_tid','=','tid')->field(array('tid','tname'))
				->where('article_aid',$v['aid'])->get();
			}
			
			
		}
			
		
		
		View::with('page',$page);
		View::with('headData',$headData);
		View::with('artData',$artData);
		View::make();
	}
	
	
	
	
	
}